﻿
using Avalonia;
using FantomNext.Controls;
using FantomNext.Shared;
using System.Threading.Tasks;

namespace FantomNext.Presenters
{
    public class ShapePresenter : IPresenter
    {
        #region fields

        private FantomShape _view;
        private Shape _model;

        #endregion

        #region props

        public FantomShape View => _view;
        public Shape Model => _model;

        public string Id => _model.Id;

        public Vector2 SceneSize
        {
            get
            {
                return _model.Size;
            }
            set
            {
                if (_model.Size == value) return;
                _model.Size = value;

                _view.Height = _model.Size.Y;
                _view.Width = _model.Size.X;
            }
        }
        public Vector2 ScenePosition
        {
            get
            {
                return _model.Position;
            }
            set
            {
                if (_model.Position == value) return;
                _model.Position = value;

                _view.TranslateTransform.X = value.X;
                _view.TranslateTransform.Y = value.Y;
            }
        }

        #endregion

        #region ctors

        public ShapePresenter(FantomShape view, Shape model)
        {
            _view = view;
            _model = model;
        }

        #endregion

        #region methods
        public void Load()
        {
            _view.Id = _model.Id;

            _view.Height = _model.Size.Y;
            _view.Width = _model.Size.X;

            _view.TranslateTransform.X = _model.Position.X;
            _view.TranslateTransform.Y = _model.Position.Y;

            _view.RenderTransformOrigin = RelativePoint.Center;

            //_view.ScaleTransform.ScaleX = 0.5;
            //_view.ScaleTransform.ScaleY = 0.5;
        }

        public async Task LoadAsync()
        {
            Load();
        }

        #endregion

        #region methods II

        /// <summary>
        /// 获得UI上绘制的框的尺寸。
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public Rect GetVisualRect(ScenePresenter parent)
        {
            Rect sceneRect = GetSceneRect();

            var p1 = sceneRect.TopLeft;
            var p3 = sceneRect.BottomRight;

            var q1 = parent.ConvertScenePointToVisualPoint(p1);
            var q3 = parent.ConvertScenePointToVisualPoint(p3);

            return new Rect(q1, q3);
        }

  
        public Rect GetSceneRect()
        {
            var x = ScenePosition.X;
            var y = ScenePosition.Y;
            var width = SceneSize.X;
            var height = SceneSize.Y;

            var originRect = new Rect(x, y, width, height);
            return originRect;
        }


        #endregion

    }
}
